import { ref, onMounted, onUnmounted } from 'vue'

export function useWindowSize(sizeOffset = 0, hRef) {
  const windowSizeRef = ref(document.documentElement.clientHeight - sizeOffset) // 使用当前窗口高度减去偏移量作为初始值
  const updateWindowSize = () => {
    if (hRef) {
      windowSizeRef.value = document.documentElement.clientHeight - sizeOffset - hRef.value
    } else {
      windowSizeRef.value = document.documentElement.clientHeight - sizeOffset
    }
  }
  onMounted(() => {
    window.addEventListener('resize', updateWindowSize)
    updateWindowSize() // 初始化窗口大小
  })
  onUnmounted(() => {
    window.removeEventListener('resize', updateWindowSize)
  })
  return windowSizeRef
}
